﻿using FireChat.Client.Model.Message;
using FireChat.Client.Model.Message.Util;
using FireChat.Client.Session.Receiver;
using FireChat.Share.Logger;
using FireChat.Share.Web.Actions;
using FireChat.Share.Web.Model.Response;
using FireChat.Share.Web.Util;
using Newtonsoft.Json;

namespace FireChat.Client.Session.Actions.Concrete;


/// <summary>
/// 服务器发送的消息处理
/// </summary>
internal class MessageAction : IClientAction
{
    private static JsonSerializerSettings _messageChainParserSetting = new()
    {
        Converters = new List<JsonConverter>()
        {
            new MessageChianJsonConvert()
        }
    };


    public ClientActions Type => ClientActions.Message;

    public void Excute(PackageReceiver receiver)
    {
        var resp = receiver.Package.GetDataInstance<FriendMessageResponse>();
        if (resp == null) return;
        
        var chain = JsonConvert.DeserializeObject<MessageChain>(resp.Content, _messageChainParserSetting);

        ($"{resp.SenderUsername}: {chain?.GetTextMessage()}" ?? "None!").LogToConsole();
    }
}
